package com.zdky.tpl.queue;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import java.util.concurrent.CountDownLatch;

/**
 * redis配置类
 * @program: springbootdemo
 * @Date: 2019/1/25 15:20
 * @Author: Mr.Zheng
 * @Description:
 */
@Configuration
@EnableCaching //开启注解
public class RedisConfig extends CachingConfigurerSupport {

    public static final String TOPIC_4_HOURS_BEFORE_WAYBILL = "before4HoursWaybill";
    /**
     * 更新 已经支付 了的运单
     */
    public static final String TOPIC_FINISHED_PAY="finishedPay";

    /**
     * 更新3天内的运单状态
     */
    public static final String TOPIC_UPDATE_STATUS_IN_3DAYS="updateStatusIn3Days";

    /**
     * 更新发票
     */
    public static final String TOPIC_UPDATE_INVOICE="updateForInvoice";


    /**
     * 对hash类型的数据操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForHash();
    }

    /**
     * 对redis字符串类型数据操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForValue();
    }

    /**
     * 对链表类型的数据操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForList();
    }

    /**
     * 对无序集合类型的数据操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForSet();
    }

    /**
     * 对有序集合类型的数据操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForZSet();
    }

    
    @Bean
    public CountDownLatch countDownLatch() {
        return new CountDownLatch(1);
    }
    
    /**
     * 消息消费者 适配器，其中 receivedMsg为定义的消费者的消费方法，必须保持一致
     */
    @Bean
    MessageListenerAdapter listenerAdapter(ConsumerFor4Hours receiver) {
        return new MessageListenerAdapter(receiver, "receivedMsg");
    }




    /**
     * 消息消费者 适配器，其中 receivedMsg为定义的消费者的消费方法，必须保持一致
     */
    @Bean
    MessageListenerAdapter listenerAdapterForInvoice(ConsumerForInvoice consumerForInvoice) {
        return new MessageListenerAdapter(consumerForInvoice, "invoiceHandle");
    }

    /**
     * 消息消费者 适配器，其中 receivedMsg为定义的消费者的消费方法，必须保持一致
     */
    @Bean
    MessageListenerAdapter listenerAdapterForPay(ConsumerForPay consumerForPay) {
        return new MessageListenerAdapter(consumerForPay, "payHandle");
    }

    @Bean
    StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
        return new StringRedisTemplate(connectionFactory);
    }
    
    /**
     * 消息监听容器，将适配器加入， 注意此处的 topic
     */
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter listenerAdapter,
                                            MessageListenerAdapter listenerAdapterForInvoice,
                                            MessageListenerAdapter listenerAdapterForPay) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, new PatternTopic(TOPIC_4_HOURS_BEFORE_WAYBILL));
        container.addMessageListener(listenerAdapterForInvoice, new PatternTopic(TOPIC_UPDATE_INVOICE));
        container.addMessageListener(listenerAdapterForPay, new PatternTopic(TOPIC_FINISHED_PAY));
        return container;
    }
}